<template>
  <div class="app-container el-org-tree">
    <h2 style="text-align: center">
      {{ (formData.user && formData.user.nickName) || "--" }}档案资料
    </h2>
    <el-row :gutter="10" class="mb8">
      <el-col :span="6">
        工号:{{ (formData.user && formData.user.userName) || "--" }}
      </el-col>
      <el-col :span="6"> 档案编号:{{ formDataInfo.docNo || "--" }} </el-col>
      <el-col :span="6">
        所在部门:{{ (formData.user && formData.user.dept.deptName) || "--" }}
      </el-col>
      <el-col :span="6"> 当前岗位:{{ formDataInfo.join.roleName }} </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['personnel:file:docCertPage:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['personnel:file:docCertPage:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['personnel:file:docCertPage:remove']"
          >删除</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      border
      :data="deptList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="档案分类" width="100">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_doc_type"
            :value="scope.row.docTypeValue"
          />
        </template>
      </el-table-column>
      <el-table-column prop="certName" label="名称" width="260">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.certName
          }}</el-link>
        </template></el-table-column
      >
      <el-table-column
        prop="createTime"
        label="上传时间"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="createByName"
        label="上传人员"
        width="200"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['company:certificate:info']"
            @click="handleDetail(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资格证书对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="certName">
              <el-input v-model="form.certName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="档案分类" prop="docTypeValue">
              <el-select
                v-model="form.docTypeValue"
                placeholder="请选择档案分类"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="dict in dict.type.sys_doc_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="url">
              <FileUpload v-model="form.url" :fileType="fileType"></FileUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { docBaseInfo } from "@/api/finance/personnel/file.js";
import {
  listDocCert,
  docCertInfo,
  delDocCert,
  docCertAdd,
  updateDocCert,
} from "@/api/finance/personnel/docCert.js";
import { queryByPhone } from "@/api/system/user.js";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "docCert",
  dicts: ["sys_doc_type"],
  components: { Treeselect },
  data() {
    return {
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "png", "jpg", "jpeg"],
      formData: { user: null },
      formDataInfo: {},
      queryParams: {
        pageNum: 1,
        userDocBaseId: this.$route.query.id,
        pageSize: 10,
      },
      rolesName: [],
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      total: 0,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 资格证书树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        certName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        docTypeValue: [
          { required: true, message: "档案分类不能为空", trigger: "change" },
        ],
        url: [{ required: true, message: "文件不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getinfo();
    this.getList();
  },
  methods: {
    getinfo() {
      docBaseInfo(this.$route.query.id).then((response) => {
        this.formDataInfo = response.data;
        queryByPhone(response.data.phone).then((e) => {
          if (e.data.user) {
            this.formData = e.data;
            this.rolesName = e.data.roles.map((i) => i.roleName);

            this.formData.postName = e.data.posts.map((i) => i.postName).join();
          }
        });
      });
    },
    handleDetail(row) {
      window.open(row.url);
    },
    /** 查询资格证书列表 */
    getList() {
      this.loading = true;
      listDocCert(this.queryParams).then((response) => {
        this.deptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }, // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userDocCertId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        certTypeId: undefined,
        certName: undefined,
        url: undefined,
        docTypeValue: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      console.log("row");

      this.reset();
      this.open = true;
      // this.title = "添加资格证书";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const userIds = row.userDocCertId || this.ids;
      this.reset();
      docCertInfo(userIds).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资格证书";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.type = this.type;
          this.form.userDocBaseId = this.$route.query.id;
          this.form.docTypeLabel = this.dict.type.sys_doc_type.find(
            (i) => i.value === this.form.docTypeValue
          ).label;
          if (this.form.userDocCertId != undefined) {
            updateDocCert(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            docCertAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userDocCertId || this.ids;
      let e = this.deptList.find((i) => i.userDocCertId == userIds);
      console.log(e);
      this.$modal
        .confirm('是否确认删除名称为"' + e.certName + '"的数据项？')
        .then(function () {
          return delDocCert(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
